package com.controller.permission;

import com.alibaba.fastjson.JSONObject;
import com.entity.SysUser;
import com.service.SysUserService;
import com.utils.DataTables;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lanouhn
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private HttpServletRequest request;

    @RequestMapping(value = "/getPageList", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getPageList() {
        return JSONObject.toJSONString(sysUserService.getPageList(DataTables.getInstance(request, null)));
    }

    /**
     * 角色管理详情
     *
     * @param id
     * @return
     * @since 2016年9月25日 上午11:15:13
     */
    @RequestMapping(value = "/getAdminById", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getAdminById(Integer id) {
        return JSONObject.toJSONString(sysUserService.selectSysUserById(id));
    }

    /**
     * 添加用户
     *
     * @param
     * @return
     * @since 2016年8月12日 下午8:42:07
     */
    @RequestMapping(value = "/add", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String addSysMenu(@ModelAttribute SysUser sysUser) {
        //1.把密码进行md5加密
        SimpleHash md5 = new SimpleHash("MD5", sysUser.getPassword(), sysUser.getUsername(), 1024);
        //2.把加密后的密码存到数据库
        sysUser.setPassword(String.valueOf(md5));
        //3.添加当前时间为创建时间
        sysUser.setCreateTime(new Date());
        //4.创建时  状态就为启用状态
        sysUser.setState(1);
        //创建人为当前登录的管理员    首先获取
        SysUser sysUsers = (SysUser) SecurityUtils.getSubject().getPrincipal();
        Integer id = sysUsers.getId();
        sysUser.setCreateId(id);
        //把数据存到数据库
        return JSONObject.toJSONString(sysUserService.addSysUser(sysUser));
    }

    /**
     * 查询所有selectAll
     */
    @RequestMapping(value = "/selectAll", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String selectAll() {
        return JSONObject.toJSONString(sysUserService.selectSysUser());
    }

    /**
     * 更新菜单
     *
     * @param
     * @return
     * @since 2016年8月12日 下午8:42:07
     */
    @RequestMapping(value = "/update", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String updateScrap(@ModelAttribute SysUser sysUser) {
        //1.把密码进行md5加密
        SimpleHash md5 = new SimpleHash("MD5", sysUser.getPassword(), sysUser.getUsername(), 1024);
        //2.把加密后的密码存到数据库
        sysUser.setPassword(String.valueOf(md5));
        //添加当前时间为修改更新时间
        sysUser.setUpdateTime(new Date());
        //获取当前用户的id     因为shiro里面存的是当前用户的信息  所以可以直接去出来
        SysUser sysUsers = (SysUser) SecurityUtils.getSubject().getPrincipal();
        Integer id = sysUsers.getId();
        //把当前用户的ID存到修改人ID里面
        sysUser.setUpdateId(id);
        //把数据存到数据库
        return JSONObject.toJSONString(sysUserService.updateSysUser(sysUser));
    }

    /**
     * 删除用户
     *
     * @param idlist
     * @return
     * @since 2016年8月12日 下午8:42:07
     */
    @RequestMapping(value = "/del", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String delScrap(@RequestParam("idlist[]") List<Integer> idlist) {
        return JSONObject.toJSONString(sysUserService.deleteByPrimaryKeys(idlist));
    }
}
